package h0901;
/**
  * @description 最长公共前缀
  * @author 不知名帅哥
  * @date 2024/9/1 23:38
  * @version 1.0
*/
public class LongestCommonPrefix {
    public static void main(String[] args) {
        String[] strs={"flower","flres","flcds"};
        System.out.println(longestCommonPrefix(strs));
        String s="abcd";
        String s1="ab";
        System.out.println(s.startsWith(s1));
    }
    public static String longestCommonPrefix(String[] strs) {
        //设置第一个为前缀
        String prefix=strs[0];
        for (int i = 0; i < strs.length; i++) {
            String curStr = strs[i];
            while (curStr.indexOf(prefix)!=0){
                prefix=prefix.substring(0,prefix.length()-1);
                if (prefix.isEmpty()){
                    return "";
                }
            }
        }
        return prefix;
    }
}
